package com.base;

import com.ddd.wandemo.R;
import com.util.NetUtils;
import com.widget.PageLayout;

/**
 * 基于BaseActivity的刷新数据activity基类
 *
 * @param <T>
 */
public abstract class BaseRefreshDataActivity<T extends BaseRefreshContract.Presenter> extends BaseActivity<T> implements BaseRefreshContract.View {


    private PageLayout mPageLayout;

    @Override
    protected void setContentLayout() {
        mPageLayout = initPageLayout();
        setContentView(mPageLayout);
        mPageLayout.changePageState(PageLayout.LOADING);
    }

    private void checkNet() {
        if (NetUtils.isConnected(this)) {
            mPageLayout.changePageState(PageLayout.NORMAL);
            loadData();
        }else {
            onNoNet();
        }
    }


    @Override
    public void afterSetContent() {
        super.afterSetContent();
        mPageLayout.setPageLayoutClickListener(new PageLayout.PageLayoutClickListener() {
            @Override
            public void onReloadClick() {
                checkNet();
            }
        });
        checkNet();
    }

    public abstract void loadData();


    protected PageLayout initPageLayout(){
        PageLayout pageLayout = new PageLayout(this);
        return pageLayout
                .setNoDataPage(R.layout.activity_base_no_data)
                .setNoNetPage(R.layout.activity_base_no_net)
                .setLoading(R.layout.activity_base_loading)
                .setNormalPage(layoutId)
                .build();
    }


    @Override
    public void onNoNet() {
        mPageLayout.changePageState(PageLayout.NO_NET);
    }

    @Override
    public void onNoData() {
        mPageLayout.changePageState(PageLayout.NO_DATA);
    }


}
